package com.yb.module.model.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yb.framework.core.base.BaseMapper;
import com.yb.framework.core.base.BaseServiceImpl;
import com.yb.framework.core.utils.SecurityUtil;
import com.yb.framework.core.utils.SnowflakeIdWorker;
import com.yb.framework.core.utils.StringUtil;
import com.yb.framework.neo4j.service.Neo4jService;
import com.yb.module.model.domain.ModelThing;
import com.yb.module.model.domain.ModelThingShape;
import com.yb.module.model.mapper.ModelThingMapper;
import com.yb.module.model.mapper.ModelThingParamMapper;
import com.yb.module.model.mapper.ModelThingShapeMapper;
import com.yb.module.model.service.ModelThingService;

@Service
public class ModelThingServiceImpl extends BaseServiceImpl<ModelThing> implements ModelThingService {
	
	@Autowired
	private Neo4jService neo4jService;
	@Autowired
	private ModelThingMapper modelThingMapper;
	@Autowired
	private ModelThingParamMapper modelThingParamMapper;
	@Autowired
	private ModelThingShapeMapper modelThingShapeMapper;
	
	@Override
	protected BaseMapper<ModelThing> getMapper() {
		return modelThingMapper;
	}

	@Override
	public int insertThing(ModelThing modelThing) {
		// TODO Auto-generated method stub
		long id = SnowflakeIdWorker.getInstance().getId();
		neo4jService.createThingNode("thing", String.valueOf(id), modelThing.getName(), 
				String.valueOf(modelThing.getTemplateId()), StringUtil.longArray2StrArray(modelThing.getShapeIds()));
		
		modelThing.setId(id);
		modelThing.setCreateBy(SecurityUtil.getUsername());
		modelThing.setCreateTime(new Date());
		int insertResult = modelThingMapper.insert(modelThing);
		
		Long[] shapeIds = modelThing.getShapeIds();
		if(shapeIds != null && shapeIds.length > 0) {
			for (Long shapeId : shapeIds) {
				ModelThingShape modelThingShape = new ModelThingShape();
				modelThingShape.setThingId(id);
				modelThingShape.setShapeId(shapeId);
				modelThingShapeMapper.insert(modelThingShape);
			}
		}
		
		return insertResult;
	}

	@Override
	public int updateThing(ModelThing modelThing) {
		// TODO Auto-generated method stub
		neo4jService.updateNode("thing", String.valueOf(modelThing.getId()), modelThing.getName(), 
				String.valueOf(modelThing.getTemplateId()), StringUtil.longArray2StrArray(modelThing.getShapeIds()));
		
		modelThing.setUpdateBy(SecurityUtil.getUsername());
		modelThing.setUpdateTime(new Date());
		int updateResult = modelThingMapper.update(modelThing);
		
		modelThingShapeMapper.deleteByThingId(modelThing.getId());
		Long[] shapeIds = modelThing.getShapeIds();
		if(shapeIds != null && shapeIds.length > 0) {
			for (Long shapeId : shapeIds) {
				ModelThingShape modelThingShape = new ModelThingShape();
				modelThingShape.setThingId(modelThing.getId());
				modelThingShape.setShapeId(shapeId);
				modelThingShapeMapper.insert(modelThingShape);
			}
		}
		
		return updateResult;
	}

	@Override
	public int deleteThing(Long[] ids) {
		// TODO Auto-generated method stub
		for (Long id : ids) {
			neo4jService.deleteNode("thing", String.valueOf(id));
		}
		
		for (Long id : ids) {
			modelThingParamMapper.deleteByThingId(id);
			modelThingShapeMapper.deleteByThingId(id);
		}
		return modelThingMapper.deleteListByIds(ids);
	}
}